﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using fivescomplement.Models;

namespace fivescomplement.Controllers
{
    public class FacultyController : Controller
    {
        //
        // GET: /Faculty/

        public ActionResult Index()
        {
            if (isUserAuth() > 0)
            {
                if (Convert.ToInt32(Session["Type"]) == 2)
                {
                    using (var database = new C335263_teacherharvestEntities())
                    {
                        string Reference = Convert.ToString(Session["Reference"]);
                        var Facultyuser = database.GetCoursesFaculty(Reference);

                        return View(Facultyuser.ToList());
                    }
                }
            }
            else
            {
                return RedirectToAction("LogOn", "Account");
            }
            return View();
        }

        public int isUserAuth()
        {
            int returnvalue = -1;
            String authUser2 = User.Identity.Name;
            if (authUser2.Equals("") || Convert.ToInt32(Session["Type"]) != 2)
            {   
            }
            else
            {
                returnvalue = 1;
            }
            return returnvalue;
        }


        public ActionResult Delete(int id, int id2)
        {
            if (isUserAuth() > 0)
            {
                using (var database = new C335263_teacherharvestEntities())
                {
                    database.DeleteQuestion(id, id2);
                }
            }
            else
            {
                return RedirectToAction("LogOn", "Account");
            }

            return RedirectToAction("Course", new { id = id });
        }

        public ActionResult Edit(int id, int id2)
        {
            if (isUserAuth() > 0)
            {

                using (var database = new C335263_teacherharvestEntities())
                {
                    var row = database.GetQuestion(id2);
                    var onlyrow = row.First();

                    ViewData["CourseID"] = id;

                    return View(onlyrow);
                }
            }
            else
            {
                return RedirectToAction("LogOn", "Account");
            }


            return View();
        }

        [HttpPost]
        public ActionResult Edit(GetQuestion_Result model, int id)
        {
            if (isUserAuth() > 0)
            {

                using (var database = new C335263_teacherharvestEntities())
                {
                    if ((model.Question + "").Length > 0)
                    {
                        database.UpdateQuestion(model.QuestionID, model.Question, model.QuestionTypeID);
                    }
                }
            }
            else
            {
                return RedirectToAction("LogOn", "Account");
            }

            return RedirectToAction("Course", new { id = id });
        }

        public ActionResult Create(int id)
        {
            if (isUserAuth() > 0)
            {
                ViewData["CourseID"] = id;
                return View();
            }
            else
            {
                return RedirectToAction("LogOn", "Account");
            }
        }


        [HttpPost]
        public ActionResult Create(QuestionModel model, string returnUrl)
        {
            if (isUserAuth() > 0)
            {
                using (var database = new C335263_teacherharvestEntities())
                {
                    if ((model.Question + "").Length > 0)
                    {
                        database.CreateQuestion(model.CourseID, model.Question, model.QuestionTypeID);
                    }
                }
            }
            else
            {
                return RedirectToAction("LogOn", "Account");
            }

            return RedirectToAction("Course", new { id = model.CourseID });
        }

        public ActionResult Course(int id)
        {
            if (isUserAuth() > 0)
            {
                using (var database = new C335263_teacherharvestEntities())
                {
                    var Facultyuser = database.GetQuestionsFaculty(id);

                    return View(Facultyuser.ToList());
                }
            }
            else
            {
                return RedirectToAction("LogOn", "Account");
            }

        }



    }
}
